home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Multimedia / Movie3.0 / Source / xanim / aux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-02  |  7.7 KB  |  430 lines

  1. /*
  2.  *  some useful definitions for memory allocation and string
  3.  *  handling
  4.  */
  5.  
  6. #define MODNAME "aux"
  7.  
  8. #include "aux.h"
  9.  
  10. #include <sys/time.h>
  11. #include <sys/times.h>
  12. #include <sys/param.h>
  13. #ifdef IRIS
  14. #include <task.h>   /* for getpid */
  15. #endif
  16.  
  17. #if PARIX
  18. char *rindex(char *s, char c)
  19. {
  20.     char *p, *pi;
  21.     
  22.     for(pi=NULL, p=s; *p; p++) if (*p==c) pi=p;
  23.     return pi;
  24. }
  25. #endif
  26.  
  27. #ifdef CM5
  28. #include <cm/cmmd.h>  /* for timer functions */
  29. #endif
  30.  
  31. int list_mem;
  32. int mem_size;
  33. int mem_s=0, mem_pl=0, mem_grid=0, mem_bo=0, mem_nb=0;
  34.  
  35. int memsize()
  36. {
  37.     return 0;
  38. }
  39.  
  40. char *checkalloc(p, len, deflen)
  41.     char *p;
  42.     int len, deflen;
  43. {
  44.     if (p==NULL) {
  45.         p = malloc(deflen);
  46.         if (p==NULL) { 
  47.                  fprintf(stderr,"Could not allocate memory.\n");
  48.             exit(1);
  49.             }
  50.     }
  51.     if (len<=deflen) return p;
  52.     p = (char*)realloc(p,len);
  53.     if (p==NULL) { 
  54.              fprintf(stderr,"Could not extend memory.\n");
  55.         exit(1);
  56.         }
  57.     return p;
  58. }
  59.  
  60. char *copy(str)
  61.     char *str;
  62. {
  63.     char *string;
  64.     int l;
  65.     
  66.     if (str==NULL) return NULL;
  67.     l = strlen(str)+1;
  68.     string = (char *)malloc(l);
  69.     if (string==NULL) { 
  70.         fprintf(stderr,"Could not string string.\n");
  71.     exit(1);
  72.     }
  73.     strcpy(string, str);
  74.     return(string);
  75. }
  76.  
  77. char aux_buf[aux_bufsiz], aux_buf2[aux_bufsiz], *aux_bufp;
  78.  
  79. char *substr(const char *str, int ofs, int len)
  80. {
  81.     char *s;
  82.     
  83.     aux_bufnstring(s, len+1);
  84.     bcopy(str+ofs, s, len);
  85.     s[ofs+len] = '\0';
  86.     return s;
  87. }
  88.  
  89. char *stringf(char *format, ...)
  90. {
  91.     char *p;
  92.     
  93.     aux_sprintf(p, format);
  94.     return(p);
  95. }
  96.  
  97. char *copydata(str, len)
  98.     char *str;
  99.     int len;
  100. {
  101.     register char *s,*p;
  102.     
  103.     if (str==NULL) return NULL;
  104.     s = (char *)malloc(len);
  105.     if (s==NULL) { 
  106.          fprintf(stderr,"Could not allocate string.\n");
  107.     exit(1);
  108.     }
  109.     p=s; s+=len;
  110.     while (p<s) *(p++)=(*(str++));
  111.     return(s-len);
  112. }
  113.  
  114. char *append(s1, s2)
  115.     char *s1, *s2;
  116. {
  117.     register char *s;
  118.     
  119.     s = (char*)realloc(s1,strlen(s1)+strlen(s2)+1);
  120.     return strcat(s,s2);
  121. }
  122.       
  123.       
  124. FILE *ropen(fn)
  125.     char *fn;
  126. {
  127.     FILE *fd;
  128.     
  129.     if (strcmp(fn,"-")==0) fd=fdopen(0,"r"); else  fd=fopen(fn,"r");
  130.       if (fd==NULL) {
  131.         fprintf(stderr,"Could not open %s for input\n",fn);
  132.         exit(1);
  133.     }
  134.     return fd;
  135. }
  136.       
  137. FILE *wopen(fn)
  138.     char *fn;
  139. {
  140.     FILE *fd;
  141.     
  142.     if (strcmp(fn,"-")==0) fd=fdopen(1,"w"); else  fd=fopen(fn,"w");
  143.       if (fd==NULL) {
  144.         fprintf(stderr,"Could not open %s for output\n",fn);
  145.         exit(1);
  146.     }
  147.     return fd;
  148. }
  149.  
  150. int debug_flag=1;
  151. int memory_flag=0;
  152.  
  153. void warning(char *format, ...)
  154. {
  155.     char *p;
  156.  
  157.     aux_sprintf(p, format);
  158.     printf("%s Warning: %s", deb_pref, p);
  159.     fflush(stdout);
  160. }
  161.  
  162. int deb_crash=DEB_EXIT;
  163.  
  164. void error(char *format, ...)
  165. {
  166.     char *p;
  167.     
  168.     aux_sprintf(p, format);
  169.     printf("%s %s\n", deb_pref, p);
  170.     fflush(stdout);
  171.     if (deb_crash==DEB_CORE) kill(getpid(),6);
  172.     else exit(1);
  173. }
  174.  
  175. char *deb_pref="";
  176. int deb_mode=DEB_CPU;
  177. double deb_cpu=-1, deb_real=-1;
  178.  
  179. real time_temp=0.0;
  180.  
  181. double realtime()
  182. {
  183. #ifdef PARIX
  184.     return ((double)TimeNowLow())/(double)CLK_TCK_LOW;
  185. #else
  186. #ifdef CM5
  187.     if (time_temp==0.0) {
  188.         CMMD_node_timer_clear(0);
  189.         CMMD_node_timer_start(0);
  190.     }
  191.     CMMD_node_timer_stop(0);
  192.     time_temp = (real)CMMD_node_timer_elapsed(0);
  193.     CMMD_node_timer_start(0);
  194.     return time_temp;
  195. #else
  196.     struct timeval tv;
  197.     struct timezone tzv;
  198.  
  199.     gettimeofday(&tv, &tzv);
  200. /*    debug("Time: %d seconds, %d microseconds, % 15.3f\n",
  201.     tv.tv_sec, tv.tv_usec, (double)tv.tv_sec + 1e-6*(double)tv.tv_usec); */
  202.     return (double)tv.tv_sec + 1e-6*(double)tv.tv_usec;
  203. #endif
  204. #endif
  205. }
  206.  
  207. double cputime() {
  208. #ifdef CM5
  209.     return realtime();
  210. #else
  211.     struct tms buf;
  212.  
  213.     times(&buf);
  214.     return (double)buf.tms_utime/(double)CLK_TCK;
  215. #endif
  216. }
  217.  
  218.  
  219. void deb_print(char *p)
  220. {
  221.     char tform[]="%s%6.2f %s", aform[]="%s%6.2f %2d%% %s";
  222.     real ct, rt;
  223.     int eff;
  224.  
  225.     if (deb_cpu<0) deb_cpu=cputime();
  226.     if (deb_real<0) deb_real=realtime();
  227.     switch (deb_mode) {
  228.     case DEB_NONE:
  229.         printf("%s %s", deb_pref, p);
  230.         break;
  231.     case DEB_CPU:
  232.         printf(tform, deb_pref, cputime()-deb_cpu, p);
  233.         break;
  234.     case DEB_REAL:
  235.         printf(tform, deb_pref, realtime()-deb_real, p);
  236.         break;
  237.     case DEB_ALL:
  238.         ct = cputime()-deb_cpu;
  239.         rt = realtime()-deb_real;
  240.         if (rt!=0) eff = (int)(ct*100/rt); else eff=99;
  241.         if (eff>99) eff=99;
  242.         printf(aform, deb_pref, rt, eff, p);
  243.         break;
  244.     case DEB_MEM:
  245.         printf("%s %d %s", deb_pref, mem_size, p);
  246.         break;
  247.     default:
  248.         error("Illegal debugging mode %d.\n", deb_mode);
  249.     }
  250.     deb_cpu = cputime();
  251.     deb_real = realtime();
  252.     fflush(stdout);
  253. }
  254.  
  255. void pdebug(char *format, ...)
  256. {
  257.     char *p;
  258.     
  259.     if (!debug_flag) return;
  260.     aux_sprintf(p, format);
  261.     deb_print(p);
  262. }
  263.  
  264. void npdebug(char *format, ...)
  265. {
  266.     char *p;
  267.     
  268.     if (!debug_flag) return;
  269.     aux_sprintf(p, format);
  270.     printf("%s", p);
  271.     fflush(stdout);
  272. }
  273.  
  274.  
  275. #define time_check 1
  276. #define time_n1 20
  277. #define time_n2 16384
  278.  
  279. real time_cpu0[time_n1], time_real0[time_n1];
  280. real time_cpu[time_n1], time_real[time_n1];
  281. char *time_names[time_n1];
  282.  
  283. short time_ind[time_n2];
  284. short time_next;
  285. short time_last;
  286. short time_initflag=0;
  287.  
  288. void time_init(void)
  289. {
  290.     int i;
  291.  
  292.     for (i=0; i<time_n1; i++) {
  293.         time_cpu[i]=time_cpu[i]=time_cpu0[i]=time_cpu0[i]=0;
  294.         time_names[i]=NULL;
  295.     }
  296.     for (i=0; i<time_n2; i++) time_ind[i]=-1;
  297.     time_next=0;
  298.     time_last=0;
  299.     time_initflag=1;
  300. }
  301.  
  302. int time_label(char *str, int create)
  303. {
  304.     int i, j;
  305.     char *s;
  306.  
  307.     if (!time_initflag) error("Timer not initialized.");
  308.     for (i=0, s=str; *s && i<time_n2; s++) i+=(int)*s;
  309.     j = time_ind[i];
  310.     if (j<0) {
  311.     if (!create) error("Timer label \"%s\" not found.", str);
  312.     if (time_next>=time_n1) error("Too many Timer labels.");
  313.     time_ind[i] = j = time_next++;
  314.     time_names[j] = copy(str);
  315.     }
  316. #if time_check
  317.     else if (strcmp(time_names[j], str))
  318.     error("Time label conflict: %s and %s.\n", str, time_names[j]);
  319. #endif
  320.     return j;
  321. }
  322.  
  323. void time_start(char *str)
  324. {
  325.     if (!time_initflag) time_init();
  326.     time_last = time_label(str, 1);
  327.     time_cpu0[time_last] = cputime();
  328.     time_real0[time_last] = realtime();
  329. }
  330.  
  331. void time_stop(char *str)
  332. {
  333.     int i;
  334.  
  335.     i = time_label(str, 0);
  336.     time_cpu[i] += cputime() - time_cpu0[i];
  337.     time_real[i] += realtime() - time_real0[i];
  338.     if (time_last==i) time_last = -1;
  339. }
  340.  
  341. void time_reset(char *str)
  342. {
  343.     int i;
  344.  
  345.     i = time_label(str, 0);
  346.     time_cpu[i] = time_real[i] = 0;
  347. }
  348.  
  349. real time_val(char *str)
  350. {
  351.     int i;
  352.     
  353.     i = time_label(str, 0);
  354.     return time_cpu[i];
  355. }
  356.  
  357. real time_lap(char *str)
  358. {
  359.     int i;
  360.     
  361.     i = time_label(str, 0);
  362.     return cputime() - time_cpu0[i];
  363. }
  364.  
  365. char *time_string_lap(char *str)
  366. {
  367.     int i;
  368.  
  369.     i = time_label(str, 0);
  370.     return stringf("cpu: %6.2f, real: %6.2f", cputime() - time_cpu0[i], realtime() - time_real0[i]);
  371. }
  372.  
  373. void time_seq(char *s)
  374. {
  375.     if (time_last>=0) {
  376.     time_cpu[time_last] += cputime() - time_cpu0[time_last];
  377.     time_real[time_last] += realtime() - time_real0[time_last];
  378.     time_last = -1;
  379.     }
  380.     if (s) time_start(s);
  381. }
  382.  
  383. void time_print(void)
  384. {
  385.     int i;
  386.     char *s;
  387.  
  388.     if (!time_initflag) error("Timer not initialized.");
  389.     time_seq(NULL);
  390.     s = "\nCumulative timing results:\n    Label     Real       CPU\n";
  391.     for (i=0; i<time_n1; i++) {
  392.     if (!time_names[i]) break;
  393.     s = stringf("%s%8s %10.3f %10.3f\n", s, time_names[i], time_real[i], time_cpu[i]);
  394.     }
  395.     printf("%s\n", s);
  396. }
  397.  
  398.  
  399. #if SRANDOM
  400. extern int rand();
  401. #else
  402. #ifndef DEC
  403. extern long random();
  404. #endif
  405. #endif
  406.  
  407. real ran_real(void)
  408. {
  409.     real r;
  410.  
  411. #if SRANDOM
  412.     r = (real)rand()/32768.0;
  413. #else
  414.     r = (real)random()/2147483648.0;
  415. #endif
  416. /*    printf("%g\n", r);  */
  417.     return r;
  418. }
  419.  
  420. char *s_lst(int* p, int len)
  421. {
  422.     char *s;
  423.     int i;
  424.     
  425.     s = "";
  426.     for (i=0; i<len; i++) s = stringf("%s %2d", s, p[i]);
  427.     return s;
  428. }
  429.  
  430.